/*
 * Copyright (C), 2007-2018, 铭师堂教育科技有限公司
 * FileName: CmdbServiceImpl.java
 * Author:   zhaoyi
 * Date:     2018年7月13日 上午9:23:02
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.secsbrain.cloudconsole.remote.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.secsbrain.common.ErrorCodeEnum;
import com.secsbrain.common.ResponseVo;
import com.secsbrain.common.utils.DevOpsHttpUtils;
import com.secsbrain.cloudconsole.remote.CmdbRemoteService;
import com.secsbrain.cmdb.client.iface.AppInfoService;
import com.secsbrain.cmdb.client.iface.HostInfoService;
import com.secsbrain.cmdb.client.iface.OpsInfoService;
import com.secsbrain.cmdb.client.model.App;
import com.secsbrain.cmdb.client.model.AppDetail;
import com.secsbrain.cmdb.client.model.HostDetail;

/**
 * CMDB相关服务
 *
 * @author zhaoyi
 */
@Service
public class CmdbRemoteServiceImpl implements CmdbRemoteService {
	
	public static final String ENV_FLAG="prod";
	
    @Value("${spring.profiles.active}")
	private String profilesActive;
    
    @Value("${api.gateway.url.cmdb:}")
	private String cmdbGatwayUrl;
    
    @Value("${cmdb.env:}")
	private String cmdbEnv;
    
    @Autowired
    AppInfoService appInfoService;
    
    @Autowired
	OpsInfoService opsInfoService;
    
    @Autowired
	HostInfoService hostInfoService;
    
    @Autowired
    private DevOpsHttpUtils httpUtils;

	/* (non-Javadoc)
	 * @see com.secsbrain.development.remote.CmdbService#queryAppDetailById(java.lang.Long)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseVo<AppDetail> queryAppDetailById(Long appId) {
		if(!ENV_FLAG.equals(profilesActive)){
			String requestUrl=cmdbGatwayUrl+"/queryAppById";
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("appId", appId);
			String result=httpUtils.getRequest(requestUrl, cmdbEnv, params);
			if(result==null){
				return ErrorCodeEnum.ERROR_COMMON_NETWORK_ERROR.getResponseVo("网络请求失败");
			}
			ResponseVo<JSONObject> jsonVo=JSON.parseObject(result, ResponseVo.class);
			
			ResponseVo<AppDetail> responseVo=new ResponseVo<AppDetail>(null);
			BeanUtils.copyProperties(jsonVo, responseVo);
			if(jsonVo.getData()!=null){
				AppDetail appDetail=JSONObject.toJavaObject(jsonVo.getData(), AppDetail.class);
				responseVo.setData(appDetail);
			}
			return responseVo;
		}else{
			return appInfoService.queryAppDetailById(appId);
		}
		
	}
	
	/* (non-Javadoc)
	 * @see com.secsbrain.development.remote.CmdbService#queryAppDetailByIds(java.lang.Long[])
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseVo<List<AppDetail>> queryAppDetailByIds(Long[] appIds) {
		if(!ENV_FLAG.equals(profilesActive)){
			String requestUrl=cmdbGatwayUrl+"/queryAppDetailByIds";
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("appIds", appIds);
			String result=httpUtils.getRequest(requestUrl, cmdbEnv, params);
			if(result==null){
				return ErrorCodeEnum.ERROR_COMMON_NETWORK_ERROR.getResponseVo("网络请求失败");
			}
			ResponseVo<JSONObject> jsonVo=JSON.parseObject(result, ResponseVo.class);
			
			ResponseVo<List<AppDetail>> responseVo=new ResponseVo<>(null);
			BeanUtils.copyProperties(jsonVo, responseVo);
			if(jsonVo.getData()!=null){
				List<AppDetail> appList=JSON.parseArray(JSON.toJSONString(jsonVo.getData()), AppDetail.class);
				responseVo.setData(appList);
			}
			return responseVo;
		}else{
			return appInfoService.queryAppDetailByIds(appIds);
		}
	}

	/* (non-Javadoc)
	 * @see com.secsbrain.development.remote.CmdbService#queryAllAppList()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseVo<List<App>> queryAllAppList() {
		if(!ENV_FLAG.equals(profilesActive)){
			String requestUrl=cmdbGatwayUrl+"/queryAllAppList";
			Map<String, Object> params=new HashMap<String, Object>();
			String result=httpUtils.getRequest(requestUrl, cmdbEnv, params);
			if(result==null){
				return ErrorCodeEnum.ERROR_COMMON_NETWORK_ERROR.getResponseVo("网络请求失败");
			}
			ResponseVo<JSONObject> jsonVo=JSON.parseObject(result, ResponseVo.class);
			
			ResponseVo<List<App>> responseVo=new ResponseVo<>(null);
			BeanUtils.copyProperties(jsonVo, responseVo);
			if(jsonVo.getData()!=null){
				List<App> appList=JSON.parseArray(JSON.toJSONString(jsonVo.getData()), App.class);
				responseVo.setData(appList);
			}
			return responseVo;
		}else{
			return appInfoService.queryAllAppList();
		}
	}

	/* (non-Javadoc)
	 * @see com.secsbrain.development.remote.CmdbService#queryEnvList()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseVo<List<String>> queryEnvList() {
		if(!ENV_FLAG.equals(profilesActive)){
			String requestUrl=cmdbGatwayUrl+"/queryEnvList";
			Map<String, Object> params=new HashMap<String, Object>();
			String result=httpUtils.getRequest(requestUrl, cmdbEnv, params);
			if(result==null){
				return ErrorCodeEnum.ERROR_COMMON_NETWORK_ERROR.getResponseVo("网络请求失败");
			}
			ResponseVo<JSONObject> jsonVo=JSON.parseObject(result, ResponseVo.class);
			
			ResponseVo<List<String>> responseVo=new ResponseVo<>(null);
			BeanUtils.copyProperties(jsonVo, responseVo);
			if(jsonVo.getData()!=null){
				List<String> envList=JSON.parseArray(JSON.toJSONString(jsonVo.getData()), String.class);
				responseVo.setData(envList);
			}
			return responseVo;
		}else{
			return opsInfoService.queryEnvList();
		}
	}
	
	/* (non-Javadoc)
	 * @see com.secsbrain.development.remote.CmdbRemoteService#queryEnvListByFlag(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseVo<List<String>> queryEnvListByFlag(String flag) {
		if(!ENV_FLAG.equals(profilesActive)){
			String requestUrl=cmdbGatwayUrl+"/queryEnvListByFlag";
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("flag", flag);
			String result=httpUtils.getRequest(requestUrl, cmdbEnv, params);
			if(result==null){
				return ErrorCodeEnum.ERROR_COMMON_NETWORK_ERROR.getResponseVo("网络请求失败");
			}
			ResponseVo<JSONObject> jsonVo=JSON.parseObject(result, ResponseVo.class);
			
			ResponseVo<List<String>> responseVo=new ResponseVo<>(null);
			BeanUtils.copyProperties(jsonVo, responseVo);
			if(jsonVo.getData()!=null){
				List<String> envList=JSON.parseArray(JSON.toJSONString(jsonVo.getData()), String.class);
				responseVo.setData(envList);
			}
			return responseVo;
		}else{
			return opsInfoService.queryEnvListByFlag(flag);
		}
	}

	/* (non-Javadoc)
	 * @see com.secsbrain.development.remote.CmdbService#queryHostDetailById(java.lang.Long)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseVo<HostDetail> queryHostDetailById(Long hostId) {
		if(!ENV_FLAG.equals(profilesActive)){
			String requestUrl=cmdbGatwayUrl+"/queryHostDetailById";
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("hostId", hostId);
			String result=httpUtils.getRequest(requestUrl, cmdbEnv, params);
			if(result==null){
				return ErrorCodeEnum.ERROR_COMMON_NETWORK_ERROR.getResponseVo("网络请求失败");
			}
			ResponseVo<JSONObject> jsonVo=JSON.parseObject(result, ResponseVo.class);
			
			ResponseVo<HostDetail> responseVo=new ResponseVo<HostDetail>(null);
			BeanUtils.copyProperties(jsonVo, responseVo);
			if(jsonVo.getData()!=null){
				HostDetail hostDetail=JSONObject.toJavaObject(jsonVo.getData(), HostDetail.class);
				responseVo.setData(hostDetail);
			}
			return responseVo;
		}else{
			return hostInfoService.queryHostDetailById(hostId);
		}
	}

	/* (non-Javadoc)
	 * @see com.secsbrain.development.remote.CmdbService#queryAllHost()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseVo<List<HostDetail>> queryAllHost() {
		if(!ENV_FLAG.equals(profilesActive)){
			String requestUrl=cmdbGatwayUrl+"/queryAllHost";
			Map<String, Object> params=new HashMap<String, Object>();
			String result=httpUtils.getRequest(requestUrl, cmdbEnv, params);
			if(result==null){
				return ErrorCodeEnum.ERROR_COMMON_NETWORK_ERROR.getResponseVo("网络请求失败");
			}
			ResponseVo<JSONObject> jsonVo=JSON.parseObject(result, ResponseVo.class);
			
			ResponseVo<List<HostDetail>> responseVo=new ResponseVo<>(null);
			BeanUtils.copyProperties(jsonVo, responseVo);
			if(jsonVo.getData()!=null){
				List<HostDetail> hostDetailList=JSON.parseArray(JSON.toJSONString(jsonVo.getData()), HostDetail.class);
				responseVo.setData(hostDetailList);
			}
			return responseVo;
		}else{
			return hostInfoService.queryAllHost();
		}
	}
	
	/* (non-Javadoc)
	 * @see com.secsbrain.development.remote.CmdbRemoteService#queryQualityByAppId(java.lang.Long)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseVo<Long> queryQualityByAppId(Long appId) {
		if(!ENV_FLAG.equals(profilesActive)){
			String requestUrl=cmdbGatwayUrl+"/queryQualityByAppId";
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("appId", appId);
			String result=httpUtils.getRequest(requestUrl, cmdbEnv, params);
			if(result==null){
				return ErrorCodeEnum.ERROR_COMMON_NETWORK_ERROR.getResponseVo("网络请求失败");
			}
			ResponseVo<Long> responseVo=JSON.parseObject(result, ResponseVo.class);
			responseVo.setData(Long.parseLong(responseVo.getData()+""));
			return responseVo;
		}
		
		return appInfoService.queryQualityByAppId(appId);
	}

	/* (non-Javadoc)
	 * @see com.secsbrain.development.remote.CmdbRemoteService#queryEnvFlag(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseVo<String> queryEnvFlag(String envName) {
		if(!ENV_FLAG.equals(profilesActive)){
			String requestUrl=cmdbGatwayUrl+"/queryEnvFlag";
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("envName", envName);
			String result=httpUtils.getRequest(requestUrl, cmdbEnv, params);
			if(result==null){
				return ErrorCodeEnum.ERROR_COMMON_NETWORK_ERROR.getResponseVo("网络请求失败");
			}
			ResponseVo<String> jsonVo=JSON.parseObject(result, ResponseVo.class);
			return jsonVo;
		}
		
		return appInfoService.queryEnvFlag(envName);
	}

}
